<?php

namespace App\Library\FormFields;

use Kris\LaravelFormBuilder\Fields\FormField;
use Kris\LaravelFormBuilder\Form;

class VideoType extends FormField
{
    private $showPreview;
    private $async;
    private $url;
    private $config;

    public function __construct(string $name, string $type, Form $parent, array $options = [])
    {
        parent::__construct($name, $type, $parent, $options);
        $this->parent->setFormOption('files', true);

        $this->showPreview = $options['show_preview'] ?? true;
        $this->async = $options['async'] ?? false;
        $this->url = $options['url'] ?? null;
        $this->config = $options['config'];
    }

    /**
     * @inheritDoc
     */
    protected function getTemplate()
    {
        return 'vendor.laravel-form-builder.video';
    }

    /**
     * @inheritDoc
     */
    public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
    {
        return parent::render($options, $showLabel, $showField, $showError);
    }

    public function getRenderData()
    {
        if (is_null($this->getValue())) {
            $this->setValue(\Form::old($this->name));
        }

        return [
            'showPreview' => $this->showPreview,
            'async' => $this->async,
            'url' => $this->url ?: $this->getValue(),
            'config' => $this->config,
        ];
    }
}
